<template>
  <div class="search-container">
    <form action="/">
      <van-search
        v-model="value"
        ref="search"
        show-action
        placeholder="请输入搜索关键词"
        background="#3296fa"
        @search="onSearch"
        @cancel="onCancel"
        @focus="onFocus"
        @input="onInput"
      />
    </form>
    <searchHistory
      v-if="historyShow"
      :searchHistory="searchHistory"
      @changeValue="change($event)"
      @delete="delet($event)"
      @delAll="delAll"
    ></searchHistory>
    <searchSuggestion
      :searchValue="value"
      v-if="suggestionShow"
      @changeValue="change($event)"
    ></searchSuggestion>
    <searchResult v-if="resultShow" :searchValue="value"></searchResult>
  </div>
</template>
<script>
import searchHistory from './search-histoty'
import searchSuggestion from './search-suggestion'
import searchResult from './search-result'
import { setItem, getItem } from '@/utils/storage'

export default {
  created() {
    this.$nextTick(function() {
      document.querySelector('.van-field__control').focus()
    })
    this.doubleSearch = getItem('history') || []
    this.searchHistory = new Set(this.doubleSearch)
  },
  computed: {},
  data() {
    return {
      value: '',
      historyShow: false,
      suggestionShow: false,
      resultShow: false,
      searchHistory: [],
      doubleSearch: []
    }
  },
  watch: {
    doubleSearch(val) {
      setItem('history', val)
    }
  },

  methods: {
    delAll() {
      this.doubleSearch = []
      this.searchHistory = new Set(this.doubleSearch)
    },
    delet(index) {
      this.doubleSearch.splice(index, 1)
      this.searchHistory = new Set(this.doubleSearch)
    },
    change(item) {
      this.value = item
      this.onSearch()
    },
    onInput(val) {
      if (this.value.length === 0) {
        this.historyShow = true
        this.suggestionShow = false
        this.resultShow = false
      } else {
        this.historyShow = false
        this.suggestionShow = true
        this.resultShow = false
      }
    },
    onFocus() {
      if (this.value.length !== 0) {
        this.historyShow = false
        this.suggestionShow = true
        this.resultShow = false
      } else {
        this.historyShow = true
        this.suggestionShow = false
        this.resultShow = false
      }
    },
    onSearch() {
      this.historyShow = false
      this.suggestionShow = false
      this.resultShow = true
      this.doubleSearch.unshift(this.value)
      this.searchHistory = new Set(this.doubleSearch)
      this.doubleSearch = Array.from(this.searchHistory)
    },
    onCancel() {
      this.$router.push('/')
    }
  },
  components: {
    searchHistory,
    searchResult,
    searchSuggestion
  }
}
</script>
<style lang="less" scoped>
.search-container {
  .search-result,
  .search-history,
  .search-container,
  .search-suggestion {
    padding-top: 108px;
  }
  .van-search {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1;
  }
  .van-search__action {
    color: #fff;
  }
}
</style>
